home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Modules / mathmodule.c < prev    next >
C/C++ Source or Header  |  1999-04-27  |  10KB  |  347 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Math module -- standard C math library functions, pi and e */
  33.  
  34. #include "Python.h"
  35.  
  36. #include "mymath.h"
  37.  
  38. #include "protos/mathmodule.h"
  39.  
  40. #ifndef _MSC_VER
  41. #ifndef __STDC__
  42. extern double fmod Py_PROTO((double, double));
  43. extern double frexp Py_PROTO((double, int *));
  44. extern double ldexp Py_PROTO((double, int));
  45. extern double modf Py_PROTO((double, double *));
  46. #endif /* __STDC__ */
  47. #endif /* _MSC_VER */
  48.  
  49.  
  50. #ifdef i860
  51. /* Cray APP has bogus definition of HUGE_VAL in <math.h> */
  52. #undef HUGE_VAL
  53. #endif
  54.  
  55. #ifdef HUGE_VAL
  56. #define CHECK(x) if (errno != 0) ; \
  57.     else if (-HUGE_VAL <= (x) && (x) <= HUGE_VAL) ; \
  58.     else errno = ERANGE
  59. #else
  60. #define CHECK(x) /* Don't know how to check */
  61. #endif
  62.  
  63. static PyObject *
  64. math_error()
  65. {
  66.     if (errno == EDOM)
  67.         PyErr_SetString(PyExc_ValueError, "math domain error");
  68.     else if (errno == ERANGE)
  69.         PyErr_SetString(PyExc_OverflowError, "math range error");
  70.     else
  71.                 /* Unexpected math error */
  72.         PyErr_SetFromErrno(PyExc_ValueError);
  73.     return NULL;
  74. }
  75.  
  76. #ifdef __SASC
  77. static PyObject *math_post_f(double x)
  78. {
  79.     CHECK(x);
  80.     if(errno!=0) return math_error();
  81.     else return PyFloat_FromDouble(x);
  82. }
  83.  
  84. #define FUNC1(stubname, func, docstring_name, docstring) \
  85.     static PyObject * stubname(PyObject *self, PyObject*args) { \
  86.     double x; if (!PyArg_Parse(args, "d", &x)) return NULL; \
  87.     errno = 0; PyFPE_START_PROTECT("in math_1", return 0) \
  88.     x=func(x);     PyFPE_END_PROTECT(x) return math_post_f(x); } \
  89.         static char docstring_name [] = docstring;
  90.  
  91. #define FUNC2(stubname, func, docstring_name, docstring) \
  92.     static PyObject * stubname(PyObject *self, PyObject *args) { \
  93.     double x, y; if(!PyArg_Parse(args, "(dd)", &x, &y)) return NULL; \
  94.     errno = 0; PyFPE_START_PROTECT("in math_2", return 0) \
  95.     x = func(x, y); PyFPE_END_PROTECT(x) return math_post_f(x); } \
  96.         static char docstring_name [] = docstring;
  97. #else /* !__SASC */
  98. static PyObject *
  99. math_1(args, func)
  100.     PyObject *args;
  101.     double (*func) Py_FPROTO((double));
  102. {
  103.     double x;
  104.     if (!  PyArg_Parse(args, "d", &x))
  105.         return NULL;
  106.     errno = 0;
  107.     PyFPE_START_PROTECT("in math_1", return 0)
  108.     x = (*func)(x);
  109.     PyFPE_END_PROTECT(x)
  110.     CHECK(x);
  111.     if (errno != 0)
  112.         return math_error();
  113.     else
  114.         return PyFloat_FromDouble(x);
  115. }
  116.  
  117. static PyObject *
  118. math_2(args, func)
  119.     PyObject *args;
  120.     double (*func) Py_FPROTO((double, double));
  121. {
  122.     double x, y;
  123.     if (! PyArg_Parse(args, "(dd)", &x, &y))
  124.         return NULL;
  125.     errno = 0;
  126.     PyFPE_START_PROTECT("in math_2", return 0)
  127.     x = (*func)(x, y);
  128.     PyFPE_END_PROTECT(x)
  129.     CHECK(x);
  130.     if (errno != 0)
  131.         return math_error();
  132.     else
  133.         return PyFloat_FromDouble(x);
  134. }
  135.  
  136. #ifdef HAVE_PROTOTYPES
  137. #define FUNC1(stubname, func, docstring_name, docstring) \
  138.     static PyObject * stubname(PyObject *self, PyObject *args) { \
  139.         return math_1(args, func); \
  140.     }\
  141.         static char docstring_name [] = docstring;
  142.  
  143. #define FUNC2(stubname, func, docstring_name, docstring) \
  144.     static PyObject * stubname(PyObject *self, PyObject *args) { \
  145.         return math_2(args, func); \
  146.     }\
  147.         static char docstring_name [] = docstring;
  148. #else /* !HAVE_PROTOTYPES */
  149. #define FUNC1(stubname, func, docstring_name, docstring) \
  150.     static PyObject * stubname(self, args) PyObject *self, *args; { \
  151.         return math_1(args, func); \
  152.     }\
  153.         static char docstring_name [] = docstring;
  154.  
  155. #define FUNC2(stubname, func, docstring_name, docstring) \
  156.     static PyObject * stubname(self, args) PyObject *self, *args; { \
  157.         return math_2(args, func); \
  158.     }\
  159.         static char docstring_name [] = docstring;
  160. #endif /* HAVE_PROTOTYPES */
  161.  
  162. #endif /* !__SASC */
  163.  
  164. FUNC1(math_acos, acos, math_acos_doc,
  165.       "acos(x)\n\nReturn the arc cosine of x.")
  166. FUNC1(math_asin, asin, math_asin_doc,
  167.       "asin(x)\n\nReturn the arc sine of x.")
  168. FUNC1(math_atan, atan, math_atan_doc,
  169.       "atan(x)\n\nReturn the arc tangent of x.")
  170. FUNC2(math_atan2, atan2, math_atan2_doc,
  171.       "atan2(y, x)\n\nReturn atan(y/x).")
  172. FUNC1(math_ceil, ceil, math_ceil_doc,
  173.       "ceil(x)\n\nReturn the ceiling of x as a real.")
  174. FUNC1(math_cos, cos, math_cos_doc,
  175.       "cos(x)\n\nReturn the cosine of x.")
  176. FUNC1(math_cosh, cosh, math_cosh_doc,
  177.       "cosh(x)\n\nReturn the hyperbolic cosine of x.")
  178. FUNC1(math_exp, exp, math_exp_doc,
  179.       "exp(x)\n\nReturn e raised to the power of x.")
  180. FUNC1(math_fabs, fabs, math_fabs_doc,
  181.       "fabs(x)\n\nReturn the absolute value of the real x.")
  182. FUNC1(math_floor, floor, math_floor_doc,
  183.       "floor(x)\n\nReturn the floor of x as a real.")
  184. FUNC2(math_fmod, fmod, math_fmod_doc,
  185.       "fmod(x,y)\n\nReturn x % y.")
  186. FUNC2(math_hypot, hypot, math_hypot_doc,
  187.       "hypot(x,y)\n\nReturn the Euclidean distance, sqrt(x*x + y*y).")
  188. FUNC1(math_log, log, math_log_doc,
  189.       "log(x)\n\nReturn the natural logarithm of x.")
  190. FUNC1(math_log10, log10, math_log10_doc,
  191.       "log10(x)\n\nReturn the base-10 logarithm of x.")
  192. #ifdef MPW_3_1 /* This hack is needed for MPW 3.1 but not for 3.2 ... */
  193. FUNC2(math_pow, power, math_pow_doc,
  194.       "power(x,y)\n\nReturn x**y.")
  195. #else
  196. FUNC2(math_pow, pow, math_pow_doc,
  197.       "pow(x,y)\n\nReturn x**y.")
  198. #endif
  199. FUNC1(math_sin, sin, math_sin_doc,
  200.       "sin(x)\n\nReturn the sine of x.")
  201. FUNC1(math_sinh, sinh, math_sinh_doc,
  202.       "sinh(x)\n\nReturn the hyperbolic sine of x.")
  203. FUNC1(math_sqrt, sqrt, math_sqrt_doc,
  204.       "sqrt(x)\n\nReturn the square root of x.")
  205. FUNC1(math_tan, tan, math_tan_doc,
  206.       "tan(x)\n\nReturn the tangent of x.")
  207. FUNC1(math_tanh, tanh, math_tanh_doc,
  208.       "tanh(x)\n\nReturn the hyperbolic tangent of x.")
  209.  
  210.  
  211. static PyObject *
  212. math_frexp(self, args)
  213.     PyObject *self;
  214.     PyObject *args;
  215. {
  216.     double x;
  217.     int i;
  218.     if (! PyArg_Parse(args, "d", &x))
  219.         return NULL;
  220.     errno = 0;
  221.     x = frexp(x, &i);
  222.     CHECK(x);
  223.     if (errno != 0)
  224.         return math_error();
  225.     return Py_BuildValue("(di)", x, i);
  226. }
  227.  
  228. static char math_frexp_doc [] =
  229. "frexp(x)\n\
  230. \n\
  231. Return the matissa and exponent for x. The mantissa is positive.";
  232.  
  233.  
  234. static PyObject *
  235. math_ldexp(self, args)
  236.     PyObject *self;
  237.     PyObject *args;
  238. {
  239.     double x, y;
  240.     /* Cheat -- allow float as second argument */
  241.         if (! PyArg_Parse(args, "(dd)", &x, &y))
  242.         return NULL;
  243.     errno = 0;
  244.     PyFPE_START_PROTECT("ldexp", return 0)
  245.     x = ldexp(x, (int)y);
  246.     PyFPE_END_PROTECT(x)
  247.     CHECK(x);
  248.     if (errno != 0)
  249.         return math_error();
  250.     else
  251.         return PyFloat_FromDouble(x);
  252. }
  253.  
  254. static char math_ldexp_doc [] = 
  255. "ldexp_doc(x, i)\n\
  256. \n\
  257. Return x * (2**i).";
  258.  
  259.  
  260. static PyObject *
  261. math_modf(self, args)
  262.     PyObject *self;
  263.     PyObject *args;
  264. {
  265.     double x, y;
  266.     if (! PyArg_Parse(args, "d", &x))
  267.         return NULL;
  268.     errno = 0;
  269. #ifdef MPW /* MPW C modf expects pointer to extended as second argument */
  270. {
  271.     extended e;
  272.     x = modf(x, &e);
  273.     y = e;
  274. }
  275. #else
  276.     x = modf(x, &y);
  277. #endif
  278.     CHECK(x);
  279.     if (errno != 0)
  280.         return math_error();
  281.     return Py_BuildValue("(dd)", x, y);
  282. }
  283.  
  284. static char math_modf_doc [] =
  285. "modf(x)\n\
  286. \n\
  287. Return the fractional and integer parts of x. Both results carry the sign\n\
  288. of x.  The integer part is returned as a real.";
  289.  
  290.  
  291. static PyMethodDef math_methods[] = {
  292.     {"acos",    math_acos,    0,    math_acos_doc},
  293.     {"asin",    math_asin,    0,    math_asin_doc},
  294.     {"atan",    math_atan,    0,    math_atan_doc},
  295.     {"atan2",    math_atan2,    0,    math_atan2_doc},
  296.     {"ceil",    math_ceil,    0,    math_ceil_doc},
  297.     {"cos",        math_cos,    0,    math_cos_doc},
  298.     {"cosh",    math_cosh,    0,    math_cosh_doc},
  299.     {"exp",        math_exp,    0,    math_exp_doc},
  300.     {"fabs",    math_fabs,    0,    math_fabs_doc},
  301.     {"floor",    math_floor,    0,    math_floor_doc},
  302.     {"fmod",    math_fmod,    0,    math_fmod_doc},
  303.     {"frexp",    math_frexp,    0,    math_frexp_doc},
  304.     {"hypot",    math_hypot,    0,    math_hypot_doc},
  305.     {"ldexp",    math_ldexp,    0,    math_ldexp_doc},
  306.     {"log",        math_log,    0,    math_log_doc},
  307.     {"log10",    math_log10,    0,    math_log10_doc},
  308.     {"modf",    math_modf,    0,    math_modf_doc},
  309.     {"pow",        math_pow,    0,    math_pow_doc},
  310.     {"sin",        math_sin,    0,    math_sin_doc},
  311.     {"sinh",    math_sinh,    0,    math_sinh_doc},
  312.     {"sqrt",    math_sqrt,    0,    math_sqrt_doc},
  313.     {"tan",        math_tan,    0,    math_tan_doc},
  314.     {"tanh",    math_tanh,    0,    math_tanh_doc},
  315.     {NULL,        NULL}        /* sentinel */
  316. };
  317.  
  318.  
  319. static char module_doc [] =
  320. "This module is always available.  It provides access to the\n\
  321. mathematical functions defined by the C standard.";
  322.  
  323. DL_EXPORT(void)
  324. initmath()
  325. {
  326.     PyObject *m, *d, *v;
  327.     
  328.     m = Py_InitModule3("math", math_methods, module_doc);
  329.     d = PyModule_GetDict(m);
  330.  
  331.         if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
  332.                 goto finally;
  333.     if (PyDict_SetItemString(d, "pi", v) < 0)
  334.                 goto finally;
  335.     Py_DECREF(v);
  336.  
  337.         if (!(v = PyFloat_FromDouble(exp(1.0))))
  338.                 goto finally;
  339.     if (PyDict_SetItemString(d, "e", v) < 0)
  340.                 goto finally;
  341.     Py_DECREF(v);
  342.     return;
  343.  
  344.   finally:
  345.         Py_FatalError("can't initialize math module");
  346. }
  347.